{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Dropout正则的简洁实现",
   "id": "b44b56702150787e"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-13T07:25:15.738136Z",
     "start_time": "2025-08-13T07:25:15.735715Z"
    }
   },
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from d2l import torch as d2l\n",
    "from utils_09 import *"
   ],
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-13T07:25:15.801407Z",
     "start_time": "2025-08-13T07:25:15.764141Z"
    }
   },
   "cell_type": "code",
   "source": [
    "batch_size = 256\n",
    "\n",
    "train_iter,test_iter = load_data_fashion_mnist(batch_size=256,cpu_workers=5)"
   ],
   "id": "b9689a5d34e56684",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-13T07:25:15.812410Z",
     "start_time": "2025-08-13T07:25:15.807411Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = nn.Sequential(\n",
    "    nn.Flatten(),\n",
    "    nn.Linear(784,256),\n",
    "    nn.ReLU(),\n",
    "    nn.Dropout(0.7), ## nn.Dropout()内填入随机丢弃神经元的概率\n",
    "    nn.Linear(256,128),\n",
    "    nn.ReLU(),\n",
    "    nn.Dropout(0.3),\n",
    "    nn.Linear(128,10)\n",
    ")"
   ],
   "id": "3ca8c5fbf6fdfb45",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-13T07:25:15.824138Z",
     "start_time": "2025-08-13T07:25:15.819919Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def init_weights(layers:nn.Module):\n",
    "    if isinstance(layers,nn.Linear):\n",
    "        nn.init.normal_(layers.weight,std = 0.01)\n",
    "net.apply(init_weights)"
   ],
   "id": "a3ec71b86cf6239e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Sequential(\n",
       "  (0): Flatten(start_dim=1, end_dim=-1)\n",
       "  (1): Linear(in_features=784, out_features=256, bias=True)\n",
       "  (2): ReLU()\n",
       "  (3): Dropout(p=0.7, inplace=False)\n",
       "  (4): Linear(in_features=256, out_features=128, bias=True)\n",
       "  (5): ReLU()\n",
       "  (6): Dropout(p=0.3, inplace=False)\n",
       "  (7): Linear(in_features=128, out_features=10, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-13T07:25:15.842142Z",
     "start_time": "2025-08-13T07:25:15.839143Z"
    }
   },
   "cell_type": "code",
   "source": [
    "lr = 0.1\n",
    "num_epochs = 50\n",
    "loss = nn.CrossEntropyLoss()\n",
    "trainer = torch.optim.SGD(net.parameters(),lr=lr)"
   ],
   "id": "2edada185661bd25",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-13T07:32:09.211970Z",
     "start_time": "2025-08-13T07:25:15.884228Z"
    }
   },
   "cell_type": "code",
   "source": [
    "metric = Accumulator(3)\n",
    "train_loss_record = list()\n",
    "train_acc_record = list()\n",
    "test_acc_record = list()\n",
    "for epoch in range(num_epochs):\n",
    "    for X,y in train_iter:\n",
    "        y_predict = net(X)\n",
    "        l = loss(y_predict,y)\n",
    "        trainer.zero_grad()\n",
    "        l.mean().backward()\n",
    "        trainer.step()\n",
    "        metric.add(float(l.sum()),accuracy(y_predict,y),y.numel())\n",
    "    train_metric = (metric[0]/metric[2],metric[1]/metric[2])\n",
    "    test_acc = evaluate_accuracy(net,test_iter)\n",
    "    print(f\"epoch {epoch}, train loss : {train_metric[0]}, train acc : {train_metric[1]},test_acc : {test_acc}\")\n",
    "    train_loss_record.append(train_metric[0])\n",
    "    train_acc_record.append(train_metric[1])\n",
    "    test_acc_record.append(test_acc)\n",
    "train_loss, train_acc = train_metric ## 训练结束，输出训练指标"
   ],
   "id": "a0d9ec6c28f7e878",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 0, train loss : 0.007504447036981583, train acc : 0.25801666666666667,test_acc : 0.5046\n",
      "epoch 1, train loss : 0.005591668491065502, train acc : 0.4471583333333333,test_acc : 0.6958333333333333\n",
      "epoch 2, train loss : 0.004635433030790753, train acc : 0.5460833333333334,test_acc : 0.7764833333333333\n",
      "epoch 3, train loss : 0.00405967595068117, train acc : 0.6057416666666666,test_acc : 0.7954333333333333\n",
      "epoch 4, train loss : 0.003668234563668569, train acc : 0.64624,test_acc : 0.7623166666666666\n",
      "epoch 5, train loss : 0.0033759733625584177, train acc : 0.6760583333333333,test_acc : 0.8050166666666667\n",
      "epoch 6, train loss : 0.0031466874187191327, train acc : 0.6992357142857143,test_acc : 0.8102833333333334\n",
      "epoch 7, train loss : 0.0029628008542582395, train acc : 0.7176770833333334,test_acc : 0.8461833333333333\n",
      "epoch 8, train loss : 0.002811643227990027, train acc : 0.7327351851851852,test_acc : 0.831\n",
      "epoch 9, train loss : 0.0026854315770665806, train acc : 0.7451016666666667,test_acc : 0.8544333333333334\n",
      "epoch 10, train loss : 0.002576796709001064, train acc : 0.7558212121212121,test_acc : 0.8507666666666667\n",
      "epoch 11, train loss : 0.0024821619384404687, train acc : 0.7650388888888889,test_acc : 0.83975\n",
      "epoch 12, train loss : 0.0023987748599778383, train acc : 0.7732128205128205,test_acc : 0.8702\n",
      "epoch 13, train loss : 0.002325050230306529, train acc : 0.7803845238095238,test_acc : 0.86715\n",
      "epoch 14, train loss : 0.0022589349842071535, train acc : 0.7867688888888889,test_acc : 0.8820833333333333\n",
      "epoch 15, train loss : 0.0021998845579102636, train acc : 0.79249375,test_acc : 0.8761666666666666\n",
      "epoch 16, train loss : 0.0021460872933122457, train acc : 0.7976686274509804,test_acc : 0.8704833333333334\n",
      "epoch 17, train loss : 0.0020963202668836823, train acc : 0.8025009259259259,test_acc : 0.8877833333333334\n",
      "epoch 18, train loss : 0.002050759745990498, train acc : 0.806888596491228,test_acc : 0.8798333333333334\n",
      "epoch 19, train loss : 0.0020084860239798826, train acc : 0.8109416666666667,test_acc : 0.8721666666666666\n",
      "epoch 20, train loss : 0.0019693873286602045, train acc : 0.8146515873015873,test_acc : 0.88875\n",
      "epoch 21, train loss : 0.001932509341869842, train acc : 0.8181439393939394,test_acc : 0.8621833333333333\n",
      "epoch 22, train loss : 0.0018984081053647441, train acc : 0.8213594202898551,test_acc : 0.89075\n",
      "epoch 23, train loss : 0.0018662092028392686, train acc : 0.8244409722222222,test_acc : 0.89755\n",
      "epoch 24, train loss : 0.0018358423284590244, train acc : 0.82732,test_acc : 0.9005\n",
      "epoch 25, train loss : 0.0018076377593171902, train acc : 0.8299403846153847,test_acc : 0.89575\n",
      "epoch 26, train loss : 0.001780312802642584, train acc : 0.832545061728395,test_acc : 0.8884166666666666\n",
      "epoch 27, train loss : 0.0017545368875953413, train acc : 0.8349732142857142,test_acc : 0.8388166666666667\n",
      "epoch 28, train loss : 0.0017301225159445713, train acc : 0.8372793103448276,test_acc : 0.8989333333333334\n",
      "epoch 29, train loss : 0.0017066925059921211, train acc : 0.8394938888888889,test_acc : 0.8777833333333334\n",
      "epoch 30, train loss : 0.0016842700136284674, train acc : 0.8415962365591397,test_acc : 0.9034166666666666\n",
      "epoch 31, train loss : 0.001662669075241623, train acc : 0.8436296875,test_acc : 0.91345\n",
      "epoch 32, train loss : 0.0016419538276514622, train acc : 0.8455833333333334,test_acc : 0.9039666666666667\n",
      "epoch 33, train loss : 0.0016220904456415012, train acc : 0.8474857843137255,test_acc : 0.8957\n",
      "epoch 34, train loss : 0.001603135336296899, train acc : 0.8492614285714286,test_acc : 0.9035333333333333\n",
      "epoch 35, train loss : 0.0015848990932175958, train acc : 0.8509856481481481,test_acc : 0.9077666666666667\n",
      "epoch 36, train loss : 0.001566912088986184, train acc : 0.8526752252252252,test_acc : 0.9159833333333334\n",
      "epoch 37, train loss : 0.0015499557767259446, train acc : 0.8542592105263158,test_acc : 0.9096\n",
      "epoch 38, train loss : 0.001533533335891035, train acc : 0.8557948717948718,test_acc : 0.9128\n",
      "epoch 39, train loss : 0.0015173426564099887, train acc : 0.8573275,test_acc : 0.9108166666666667\n",
      "epoch 40, train loss : 0.0015020874436914437, train acc : 0.8587495934959349,test_acc : 0.9249166666666667\n",
      "epoch 41, train loss : 0.0014871755035741935, train acc : 0.8601444444444445,test_acc : 0.9176166666666666\n",
      "epoch 42, train loss : 0.0014727432613282703, train acc : 0.8615096899224807,test_acc : 0.9012666666666667\n",
      "epoch 43, train loss : 0.0014584965197933894, train acc : 0.8628526515151516,test_acc : 0.9199166666666667\n",
      "epoch 44, train loss : 0.0014448001130946257, train acc : 0.8641314814814814,test_acc : 0.91795\n",
      "epoch 45, train loss : 0.0014316139785194959, train acc : 0.8653670289855072,test_acc : 0.9201\n",
      "epoch 46, train loss : 0.0014184222104780852, train acc : 0.8666283687943263,test_acc : 0.9204333333333333\n",
      "epoch 47, train loss : 0.001405481447160451, train acc : 0.8678611111111111,test_acc : 0.9191166666666667\n",
      "epoch 48, train loss : 0.0013930297484811471, train acc : 0.8690119047619047,test_acc : 0.9037\n",
      "epoch 49, train loss : 0.001380824326681594, train acc : 0.870159,test_acc : 0.9206666666666666\n"
     ]
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
